package com.itchen.school.common.Result;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

// 通用返回结果，服务端响应的数据最终都会封装成此对象
@Data
public class R<T> {

    //编码：200为成功，400为失败
    private Integer code;

    //错误信息
    private String msg;

    //数据
    private T data;

    //动态数据
    private Map map = new HashMap();

    public static <T> R<T> success() {
        R<T> r = new R<T>();
        r.code = 200;
        return r;
    }

    public static <T> R<T> successData(T object) {
        R<T> r = new R<T>();
        r.data = object;
        r.code = 200;
        return r;
    }

    public static <T> R<T> successMsg(T object, String msg) {
        R<T> r = new R<T>();
        r.data = object;
        r.msg = msg;
        r.code = 200;
        return r;
    }

    public static <T> R<T> error() {
        R r = new R();
        r.code = 400;
        return r;
    }

    public static <T> R<T> errorMsg(String msg) {
        R r = new R();
        r.msg = msg;
        r.code = 400;
        return r;
    }

    public R<T> addMap(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
